package com.ruoyi.util.filetopdf;

import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * @Description: 创建PDF(表格)
 * @ClassName: CreatePDFTable
 * @author syc
 * @date: 2022年9月28日 下午4:16:56
 * 	参考文档 https://blog.csdn.net/weixin_43288858/article/details/124016919
 */
public class CreatePDFTable {
      
//	public static void main(String[] args) {
//		try {
//			CreatePDFTable.createPDF();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

	public  static void createPDF() throws Exception {
	    	JSONObject json = new JSONObject();
	    	json.put("yq_no", "【2022】 第215号");
	    	json.put("yq_zrdw", "建中街道");
	    	json.put("yq_ly", "网站: 顶端新闻");
	    	json.put("yq_ljdz", "http://117.160.219.19:12001/mainserver/baseserver/newWorkbenchRouter/benchRouter/Router");
	    	json.put("yq_tm", "未报联审家长电梯");
	    	json.put("yq_lrsj", "2022-09-09 11:11:11");
	    	json.put("yq_nrjj", "郑州市二七区交通路6号院7单元在没有通过社区公示，不报审加梯办材料，没有通过联审，在二分之一的业主不同意的情况下，施工方强行施工，9月8号社区主任通知停工还继续施工。110都出警了，走了继续施工，我们不知道哪个执法部门有权来阻止。现在电梯框架已按到三楼了，电梯安装是惠民工程，利国利民，为什么不通过正常手续申请安装，而且他们不申请政府补贴，安装电梯房屋不经过评估，将来出现问题谁的责任。");
	    	json.put("yq_blyq", "1、全面核实网友反映的问题是否属实。\n\t2、针对核实情况采取措施解决，如不能解决请核实情况并作出说明。\n\t" + 
	    			"3、单位主要领导亲自过问此事，同时明确具体经办人员姓名、职务、联系方式。\n\t" + 
	    			"A、09月11日上午12:00前将办理结果报区宣传部1213，书面材料要求单位主要领导（一把手）签字并加盖单位公章。\n\t" + 
	    			"注：落实情况需上报区主要领导，并进行网上回复。");
	    	json.put("yq_jbdw","区委宣传部");
	    	json.put("yq_lxdh","13300000000");
	    	json.put("yq_jbrq","2022-09-01 12:10:01");
	    
	       
	    	Paragraph paragraph = null;
	    	Document document = new Document(PageSize.A4);
	        //第二步，创建Writer实例
	        PdfWriter.getInstance(document, new FileOutputStream("D:/test123/hello12212.pdf"));
	        //创建中文字体
//	        BaseFont bfchinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//	        Font fontChinese = new Font(bfchinese, 12, Font.NORMAL);
	        //第三步，打开文档
	        document.open();
	        //第四步，写入内容
	        //1 大标题
	        paragraph = new Paragraph("网上群众路线交办单", creatFone(20, Font.BOLD));
	        paragraph.setAlignment(1); // 设置文字居中 0靠左 1，居中 2，靠右 
			document.add(paragraph);
			  //换行
			
			// 2 编号
			paragraph = new Paragraph("编号：" + json.getString("yq_no"), creatFone(15, Font.BOLD));
			paragraph.setAlignment(0);
			document.add(paragraph);
			paragraph = new Paragraph("  ", creatFone(12, Font.BOLD));
			document.add(paragraph);
	//创建一列的格子
	        PdfPTable goodTable = new PdfPTable(12);
	 
	        goodTable.setWidthPercentage(100);
	        PdfPCell cell;
	        // 1.1
	        cell = new PdfPCell(new Phrase("责任单位", creatFone(15, Font.BOLD)));
	//格子横跨2个格子
	        cell.setColspan(2);
	//格子高度35px
	        cell.setMinimumHeight(35);
	//格子纵跨1个格子
	        cell.setRowspan(1);
	//格子内容左右居中
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	//格子内容上下居中
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //1.2 
	        cell = new PdfPCell(new Phrase(json.getString("yq_zrdw"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(10);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //2.1
	        cell = new PdfPCell(new Phrase("舆情来源", creatFone(15, Font.BOLD)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	 
	        //2.2
	        cell = new PdfPCell(new Phrase(json.getString("yq_ly"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(10);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //3.1
	        cell = new PdfPCell(new Phrase("链接地址", creatFone(15, Font.BOLD)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	 
	        //3.2
	        cell = new PdfPCell(new Phrase(json.getString("yq_ljdz"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(10);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //3.1
	        cell = new PdfPCell(new Phrase("题    目", creatFone(15, Font.BOLD)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	 
	        //3.2
	        cell = new PdfPCell(new Phrase(json.getString("yq_tm"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(7);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //3.3
	        cell = new PdfPCell(new Phrase("时间", creatFone(15, Font.BOLD)));
	        cell.setColspan(1);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //3.4
	        cell = new PdfPCell(new Phrase(json.getString("yq_lrsj"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //4.1
	        cell = new PdfPCell(new Phrase("内容简介", creatFone(15, Font.BOLD)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //4.2
	        cell = new PdfPCell(new Phrase(json.getString("yq_nrjj"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(10);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        //cell.setLeading(fixedLeading,MultipledLeading);
	        cell.setLeading(2.2f,1.8f);
	        cell.setPadding(6f);
	       // cell.setFixedHeight(70);
	        goodTable.addCell(cell);
	        
	        //5.1
	        cell = new PdfPCell(new Phrase("办理要求", creatFone(15, Font.BOLD)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        //5.2
	        cell = new PdfPCell(new Phrase(json.getString("yq_blyq"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(10);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        cell.setPadding(6f);
	        cell.setLeading(2.2f,1.8f);
	        goodTable.addCell(cell);
	        
	        //6.1
	        cell = new PdfPCell(new Phrase("交办单位", creatFone(15, Font.BOLD)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        
	        // 6.2
	        cell = new PdfPCell(new Phrase(json.getString("yq_jbdw"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(4);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        //6.3
	        cell = new PdfPCell(new Phrase("联系电话", creatFone(15, Font.BOLD)));
	        cell.setColspan(2);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);  
	        //6.4
	        cell = new PdfPCell(new Phrase(json.getString("yq_lxdh"), creatFone(15, Font.NORMAL)));
	        cell.setColspan(4);
	        cell.setMinimumHeight(35);
	        cell.setRowspan(1);
	        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
	        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        goodTable.addCell(cell);
	        document.add(goodTable);
	        paragraph = new Paragraph("交办日期：" + json.getString("yq_jbrq"), creatFone(15, Font.BOLD));
			paragraph.setAlignment(2);
			document.add(paragraph);
	        //第五步，关闭文档
	        document.close();
	    } 
	    
	    
	    /**
		 * 	设置样式
		 * @throws  
		 */
		public static Font creatFone(int szie, int style) {
			try {
				return new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), szie, style);
			} catch (DocumentException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
}
